using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
   public class tbDoanhnghiepDAL
    {
        private SqlConnection cn = null;
        public tbDoanhnghiepDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbDoanhnghiep dn)
        {
            SqlCommand cm = new SqlCommand("Doanh_nghiep_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@tendnghiep", dn.Tendnghiep);
            cm.Parameters.AddWithValue("@diachi", dn.Diachi);
            cm.Parameters.AddWithValue("@matkhau", dn.Matkhau);
            cm.Parameters.AddWithValue("@tendnhap", dn.Tendnhap);
            cm.Parameters.AddWithValue("@website", dn.Website);
            cm.Parameters.AddWithValue("@dienthoai", dn.Dienthoai );
            cm.Parameters.AddWithValue("@giomocua", dn.Giomocua);
            cm.ExecuteNonQuery();
        }
        public void Update(tbDoanhnghiep dn)
        {

            SqlCommand cm = new SqlCommand("Doanh_nghiep_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madn", dn.Madn);
            cm.Parameters.AddWithValue("@tendnghiep", dn.Tendnghiep);
            cm.Parameters.AddWithValue("@diachi", dn.Diachi);
            cm.Parameters.AddWithValue("@matkhau", dn.Matkhau);
            cm.Parameters.AddWithValue("@tendnhap", dn.Tendnhap);
            cm.Parameters.AddWithValue("@website", dn.Website);
            cm.Parameters.AddWithValue("@dienthoai", dn.Dienthoai);
            cm.Parameters.AddWithValue("@giomocua", dn.Giomocua);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("Doanh_nghiep_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madn", iD);
            cm.ExecuteNonQuery();
        }
       public tbDoanhnghiepCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("Doanh_nghiep_Get", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Where", where);
           cm.Parameters.AddWithValue("@PageIndex", pageIndex);
           cm.Parameters.AddWithValue("@PageSize", pageSize);
           cm.Parameters.AddWithValue("@OrderBy", orderBy);
           cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
           cm.Parameters.Add(new SqlParameter("@TotalRecords",DbType.Int32)).Direction = ParameterDirection.Output;
           SqlDataReader dr = cm.ExecuteReader();
           tbDoanhnghiepCollection dnCollection = new tbDoanhnghiepCollection();
           while (dr.Read())
           {
               tbDoanhnghiep dn = new tbDoanhnghiep();
               dn.Madn = int.Parse(dr.GetValue(0).ToString());
               dn.Tendnghiep = dr.GetValue(1).ToString();
               dn.Diachi = dr.GetValue(2).ToString();
               dn.Matkhau = dr.GetValue(3).ToString();
               dn.Tendnhap = dr.GetValue(4).ToString();
               dn.Website = dr.GetValue(5).ToString();
               dn.Dienthoai = dr.GetValue(6).ToString();
               dn.Giomocua = dr.GetValue(7).ToString();
               dnCollection.Add(dn);
           }
           dr.Close();
           totalRecords = int.Parse(
           cm.Parameters["@TotalRecords"].Value.ToString());
           return dnCollection;
       }
       public tbDoanhnghiep GetByTendangnhap(string tdn)
       {
           string cautruyvan = "select * from Doanh_nghiep WHERE tendnhap='" + tdn + "'";
           SqlDataAdapter da = new SqlDataAdapter(cautruyvan, cn);
           DataTable tb = new DataTable("Doanh_nghiep");
           try
           {
               da.Fill(tb);
           }
           catch
           {
               return null;
           }
           tbDoanhnghiep dn = new tbDoanhnghiep();
           if (tb.Rows.Count > 0)
           {
               DataRow dr = tb.Rows[0];
               dn.Madn = int.Parse(dr["Madn"].ToString());
               dn.Tendnhap = dr["Tendnghiep"].ToString();           
               dn.Diachi = dr["Diachi"].ToString();
               dn.Matkhau = dr["Matkhau"].ToString();
               dn.Tendnghiep = dr["Tendnhap"].ToString();
               dn.Website = dr["Website"].ToString();
               dn.Dienthoai = dr["Dienthoai"].ToString();
               dn.Giomocua = dr["Giomocua"].ToString();
               return dn;
           }
           return null;
       }
    }
}
